﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace UtilZ.Dotnet.Ex.Communication.Net.Telnet.KeyCommands
{
    internal class TelnetKeyCommandManager
    {
        private static readonly ReadOnlyCollection<ITelnetKeyCommand> _commands;
        public static ReadOnlyCollection<ITelnetKeyCommand> Commands
        {
            get { return _commands; }
        }

        static TelnetKeyCommandManager()
        {
            Type selfType = typeof(TelnetKeyCommandManager);
            Type[] typeArr = selfType.Assembly.GetTypes().Where(t =>
            {
                return string.Equals(t.Namespace, selfType.Namespace);
            }).ToArray();
            Type itelnetCommandType = typeof(ITelnetKeyCommand);
            ITelnetKeyCommand command;
            List<ITelnetKeyCommand> commandList = new List<ITelnetKeyCommand>();

            foreach (var type in typeArr)
            {
                if (!type.IsClass ||
                    type.IsAbstract ||
                    type.GetInterface(itelnetCommandType.Name) == null)
                {
                    continue;
                }

                command = (ITelnetKeyCommand)Activator.CreateInstance(type);
                commandList.Add(command);
            }
            _commands = new ReadOnlyCollection<ITelnetKeyCommand>(commandList);
        }

    }
}
